//
//  SNLandInformationController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/29.
//  Copyright © 2016年 stone. All rights reserved.
//
#import "SNPlanListViewController.h"
#import "SNLandInformationController.h"
#import "SNLandInformationCell.h"
#import "SNLandModel.h"
@interface SNLandInformationController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** models */
@property (nonatomic,strong) NSMutableArray<NSDictionary *> * models;

/** SNLandModel * landModel */
@property (nonatomic,strong) SNLandModel * landModel;
@end

@implementation SNLandInformationController

- (NSMutableArray<NSDictionary *> *)models {
    if (_models == nil) {
        
        NSString * filePath = [[NSBundle mainBundle] pathForResource:@"landlist.plist" ofType:nil];
        NSArray<NSDictionary *> * arr = [NSArray arrayWithContentsOfFile:filePath];
        
        _models = [arr copy];
    }
    return _models;
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"选择地块";
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(rightBarButtonItemClicked:)];
    
    [self addBackgroundView];
    [self addTableView];
}
- (void)rightBarButtonItemClicked:(UIBarButtonItem *)sender
{
    NSLog(@"%@ | %s",self.class,__func__);
    //TODO: 新建
//    if (self.productionPlanListResult.landList.count > 0) {
        NSMutableArray<SNProductionPlanListLandList *> * arrM = [NSMutableArray array];
        
        [self.landModel.result enumerateObjectsUsingBlock:^(SNLandResult * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if (obj.selected) {
                SNProductionPlanListLandList * productionPlanListLandList = [SNProductionPlanListLandList new];
                productionPlanListLandList.availableArea = obj.availableArea;
                productionPlanListLandList.landLocation = obj.landLocation;
                productionPlanListLandList.landType = obj.landType;
                productionPlanListLandList.landNo = obj.landNo;
                productionPlanListLandList.plantArea = obj.plantArea;
                [arrM addObject:productionPlanListLandList];
            }
            
        }];
        
        self.localProductionPlanListResult.landList = arrM;
        
//    }else{
//        
//    }
    NSLog(@"self.productionPlanListResult = %@",self.localProductionPlanListResult);
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
    
}
- (void)addBackgroundView
{
    CALayer * layer = [CALayer layer];
    
    /** center & bounds */
    layer.frame = CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height -64);
    /** color */
    layer.backgroundColor = kNormalBackgroundColor.CGColor;
    
    [self.view.layer addSublayer:layer];
}
- (void)addTableView
{

    /** y坐标 */
    CGFloat y = 64;
    /** 高度 tableView */
    CGFloat height = self.view.bounds.size.height - y;
    /** 样式 tableView */
    UITableViewStyle tableViewStyle = UITableViewStylePlain;
    
    CGRect frame = CGRectMake(10, y+10, self.view.bounds.size.width-20, height-20);
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:tableViewStyle];
    self.tableView = tableView;
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self setupInit:tableView];
    
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    //    tableView.separatorColor = [UIColor greenColor];
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
    NSLog(@"self.productionPlanListResult.custNo = %@",[SNPlanListViewController getPlantingDecisionMakerResult].custNo);
    [[ZXHTTPManager sharedManager] querycustavailablelandWithCustNo:[SNPlanListViewController getPlantingDecisionMakerResult].custNo address:nil completed:^(NSDictionary *jsonDict, NSError *error) {
       
        logError;
        
        NSLog(@"jsonDict = %@",jsonDict);
        
        SNLandModel * landModel = [SNLandModel mj_objectWithKeyValues:jsonDict];
        
        if (landModel.status.integerValue == 1) {
            self.landModel = landModel;
            
            [self.tableView reloadData];
            
            [self addAlertViewController:landModel.message];
        }else{
            self.landModel = landModel;
            [self filterData:landModel];
            [self.tableView reloadData];
        }
        
        
    }];
}
- (void)filterData:(SNLandModel *)landModel
{
    [landModel.result enumerateObjectsUsingBlock:^(SNLandResult * _Nonnull landResult, NSUInteger idx, BOOL * _Nonnull stop) {
//        if([landResult.landLocation isEqualToString:self.productionPlanListResult.landList[idx].landLocation]){
//            landResult.selected = YES;
//        }
        
        [self.localProductionPlanListResult.landList enumerateObjectsUsingBlock:^(SNProductionPlanListLandList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if([landResult.landLocation isEqualToString: obj.landLocation]){
                landResult.selected = YES;
            }
            
            
        }];
        
    }];
}

- (void)addAlertViewController:(NSString *)messageString
{
    NSString * title = @"";
    NSString * message = messageString;
    NSString * actionButtonTitle = @"确定";
    
    UIAlertController * alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction * action = [UIAlertAction actionWithTitle:actionButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 确定之后需要 处理的code
        
    }];
    
    [alertController addAction:action];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}
/** 初始化 tableView */
- (void)setupInit:(UITableView *)tableView
{
    //MARK: - 设置tableHeaderView
    //    {
    //        UIView * tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    //        tableView.tableHeaderView = tableHeaderView;
    //    }
    
    //MARK: - 注册
    {
        /** xib 注册cell */
            registerForCellFromeNib(tableView,SNLandInformationCell);
        /** class 注册cell */
//        registerForCellFromeClass(tableView,UITableViewCell);
        
        /** xib 注册 headerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 headerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
        
        /** xib 注册 footerSection */
        //    registerForHeaderFooterViewFromeNib(tableView,UITableViewHeaderFooterView);
        /** class 注册 footerSection */
        //    registerForHeaderFooterViewFromeClass(tableView,UITableViewHeaderFooterView);
    }
    //MARK: - 设置高度
    {

        /** cell */
        tableView.estimatedRowHeight = 100;
        tableView.rowHeight = UITableViewAutomaticDimension;

    }
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.landModel.result.count;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SNLandInformationCell * cell = DequeueForCell(SNLandInformationCell);
    //    SNTableViewCell * cell = [SNTableViewCell cellWithTableView:tableView];
//    cell.model = self.models[indexPath.row];
    cell.landReqult = self.landModel.result[indexPath.row];
//    cell.contentView.backgroundColor = indexPath.row % 2 == 0 ? [UIColor colorWithRed:247 / 255.0 green:206 / 255.0 blue:166 / 255.0 alpha:1.0] : [UIColor colorWithRed:242 / 255.0 green:154 / 255.0 blue:76 / 255.0 alpha:1.0];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    return cell;
}
#pragma mark - <UITableViewDelegate>
/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}
/** 取消 选中 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%s", __func__);
}
/** 可编辑 */
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
/** 删除样式 */
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
/** 删除当前行 */
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [self.models removeObjectAtIndex:indexPath.row];
//
//        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationLeft];
//}
@end
